//previous page id, current page id
var prevPid = '', curPid = '';
//save opened frame name
var frameArr = [];


//frame whether open
function isOpened(frmName){
    var i = 0, len = frameArr.length;
    var mark = false;
    for(i; i<len; i++){
        if(frameArr[i] === frmName){
            mark = true;
            return mark;
        }
    }
    return mark;
}

//toggle header
function showHeader(type){
    if(!type){return;}
    if(type=='car-home'){
        var header = $api.dom('#header .active');
        $api.removeCls(header,'active');  //这两个注释的是不显示新车购的头部
    }else{
        var header = $api.dom('#header .active');
        $api.removeCls(header,'active');
        var thisHeader = $api.dom('#header .'+type);
        $api.addCls(thisHeader,'active');
    }
}

var frmurl;
//open tab
function openTab(type,frmNameurl){
	  
    
    showHeader(type);
    var width = api.winWidth;
    var nav = $api.byId('nav');
    var header = $api.byId('header');
    
    var headerH = $api.offset(header).h;
	var footerH =$api.offset(footer).h;//frame的高度为当前window高度减去header和footer的高度
	var frameH =api.winHeight - headerH - footerH;
    
    if(type == "sales_informn_con" || type == "change_informn_con" 
        || type=="car_informn_con" || type=="assess_informn_con" || type=="verficat_informn_con"||type=="car_dealer_con"){
    	$api.rmStorage(type+'_msid');
    	$api.setStorage(type+'_msid','display: none');
    	$api.css($api.byId('msids'),$api.getStorage(type+'_msid'));
    	api.execScript({	
			frameName: 'change_informn_con',        	
	    	script:'getdatlis();'
		});
    	
    	api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });	
    }
    
    if(frmNameurl==0){
		frmurl=type;				
	}else if(frmNameurl==1){
		cbtota();//查询授信
		frmurl='../merchants/'+type;
		api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });
	}else if(frmNameurl==2){
		//headerH=0;		
		frmurl='../salesman/'+type;
		api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });
	} else if(frmNameurl==3){
        //headerH=0;      
        frmurl=type
        api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });
    } else if(frmNameurl=='home'){
		frmurl='../release/'+type;  
        api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });
    }  else if(frmNameurl=='release-car'){
		frmurl='../release/'+type;  
        api.setFrameAttr({
            name: type,
            reload:true,
            hidden: false
        });
    }      
    
    type = type || 'activity';
    var actTab = $api.dom('#footer .active');
    $api.removeCls(actTab,'active');
    var thisTab = $api.dom('#footer .'+ type);
    //thisTab = thisTab.parentNode;
    $api.addCls(thisTab,'active');
	
    //record page id
    prevPid = curPid;
    curPid = type; 

    if(prevPid !== curPid){ 
	        if(isOpened(type)){	        	        			       
		            api.setFrameAttr({
		                name: type,
		                reload:true,
		                hidden: false
		            });	           
	        }else{	          
	            api.openFrame({
	                name: type,
		            url: frmurl+'.html',
		            bounces: false,
		            opaque: true,	                
		            vScrollBarEnabled:false,		            
		            hScrollBarEnabled:false,
		            pageParam:{footerH:footerH},
		            rect: {
		                x: 0,
		                y: headerH,
	                    w: width,
		                h: frameH
	                },
	                delay:200
	            });
	        }			
	        if(prevPid){	        		       				        
	            api.setFrameAttr({
	                name: prevPid,
	                hidden: true
	            });        
			                 
	        }
	
	        if(!isOpened(type)){
	            //save frame name
	            frameArr.push(type);//向数组的末尾添加一个或多个元素
	        }
	        
	    }
   
}

//按两次返回退出应用
 function exitApp(){ 
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err){
            if(searchoff=='1'){
            	closeFramGroup();
				searchoff='';
            }else{
           
                	api.toast({
                        msg: '再按一次退出'+api.appName,
                        duration:3000,
                        location: 'bottom'
                    });                  
                    api.addEventListener({
                        name: 'keyback'
                    }, function(ret, err){
                        api.closeWidget({
                                id: 'A6981995008179',     //这里改成自己的应用ID
                                retData: {name:'closeWidget'},
                                silent:true//关闭默认对话框
                            });
                    });                  
                    setTimeout(function(){exitApp();},3000);
                  
                }
            });
 }
